package com.uyaer.wheel.texture
{
	import com.uyaer.wheel.game.map.MapCellType;
	import com.uyaer.wheel.res.ResManager;
	import com.uyaer.wheel.utils.Utils;
	
	import flash.geom.Point;
	
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;

	/**
	 *游戏贴图对象池 
	 * @author hc
	 * 
	 */	
	public class TexturePool
	{
		private static var imgList:Vector.<Image> = new Vector.<Image>();
		private static var starList:Vector.<MovieClip> = new Vector.<MovieClip>();
		
		private static var texture:Image;
		private static var lenX:Number;
		/**
		 * 根据类型获得元素
		 * @param type MapCellType.HILL
		 * @return 
		 * 
		 */		
		public static function getOne(type:int,size:Number):Image{
			lenX = size;
			switch(type)
			{
				case MapCellType.NONE:
				case MapCellType.SPACE_HOLDER:
					return null;
				case MapCellType.ROCK:
					setHill();
					break;	
				case MapCellType.TREE1x1:
					setTree1x1();
					break;
				case MapCellType.TREE2x1:
					setTree2x1();
					break;
				case MapCellType.TREE2x2:
					setTree2x2();
					break;
				case MapCellType.TREE3x2:
					setTree3x2();
					break;
				case MapCellType.STAR:
					setStar();
					break;
				case MapCellType.POP:
					setPop();
					break;
				default:
					throw "TexturePool::getOne() 非法数值";
					break;
			}
			texture.readjustSize();
			if(type == MapCellType.ROCK){
				texture.width = lenX;
				texture.setTexCoords(1,new Point(lenX / texture.texture.width,0));
				texture.setTexCoords(3,new Point(lenX / texture.texture.width,1));
			}else{
				texture.setTexCoords(1,new Point(1,0));
				texture.setTexCoords(3,new Point(1,1));
				texture.width = texture.texture.width;
			}
			texture.pivotY = texture.height;
			return texture;
		}
		
		private static function setPop():void{
			if(imgList.length>0){
				texture = imgList.pop();
				texture.texture = ResManager.me.getTexture("map_pop");
			}else{
				texture = new Image(ResManager.me.getTexture("map_pop"));
			}
		}
		
		private static function setStar():void{
			var mc:MovieClip;
			if(starList.length > 0){
				mc = starList.pop();
			}else{
				mc = new MovieClip(ResManager.me.getTextures("map_star"),6);
			}
			mc.play();
			Starling.juggler.add(mc);
			texture = mc;
		}
		
		private static function setTree3x2():void{
			if(imgList.length>0){
				texture = imgList.pop();
				texture.texture = ResManager.me.getTexture("map_tree_3x2_"+Utils.rangInt(1,3));
			}else{
				texture = new Image(ResManager.me.getTexture("map_tree_3x2_"+Utils.rangInt(1,3)));
			}
		}
		
		private static function setTree2x2():void{
			if(imgList.length>0){
				texture = imgList.pop();
				texture.texture = ResManager.me.getTexture("map_tree_2x2_"+Utils.rangInt(1,3));
			}else{
				texture = new Image(ResManager.me.getTexture("map_tree_2x2_"+Utils.rangInt(1,3)));
			}
		}
		
		private static function setTree2x1():void{
			if(imgList.length>0){
				texture = imgList.pop();
				texture.texture = ResManager.me.getTexture("map_tree_2x1_"+Utils.rangInt(1,2));
			}else{
				texture = new Image(ResManager.me.getTexture("map_tree_2x1_"+Utils.rangInt(1,2)));
			}
		}
		
		private static function setTree1x1():void{
			if(imgList.length>0){
				texture = imgList.pop();
				texture.texture = ResManager.me.getTexture("map_tree_1x1_"+Utils.rangInt(1,11));
			}else{
				texture = new Image(ResManager.me.getTexture("map_tree_1x1_"+Utils.rangInt(1,11)));
			}
		}
		
		/**
		 *获得山的对象 
		 * @return 
		 * 
		 */		
		private static function setHill():void{
			if(imgList.length>0){
				texture = imgList.pop();
				texture.texture = ResManager.me.getTexture("map_rock_"+Utils.rangInt(1,3));
			}else{
				texture = new Image(ResManager.me.getTexture("map_rock_"+Utils.rangInt(1,3)));
			}
		}
		
		/**
		 *回收 
		 * @param type
		 * @param t
		 * 
		 */		
		public static function recycle(type:int, t:Image):void{
			if(!t)return;
			switch(type)
			{
				case MapCellType.NONE:
				case MapCellType.SPACE_HOLDER:
					break;
				case MapCellType.ROCK:
				case MapCellType.TREE1x1:
				case MapCellType.TREE2x1:
				case MapCellType.TREE2x2:
				case MapCellType.TREE3x2:
				case MapCellType.POP:
					t.removeFromParent();
					imgList.push(t);
					break;	
				case MapCellType.STAR:
					t.removeFromParent();
					starList.push(t);
					break;
				default:
					throw "TexturePool::getOne() 非法数值";
					break;
			}
			var mc:MovieClip = t as MovieClip;
			if(mc){
				mc.stop();
				Starling.juggler.remove(mc);
			}
		}
		
		public static function clear():void{
			for each (var img:Image in imgList){
				img.dispose();
			}
			for each (var star:MovieClip in starList){
				star.dispose();
			}
			imgList = new Vector.<Image>();
			starList = new Vector.<MovieClip>();
		}
	}
}